package org.huawei260.TiKu.D;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

/**
 * @Auther: qingle
 * @Date: 2024/10/27-20:35
 * @Description:
 * @version: 1.0
 */
public class D_2832_哈希表2024D_测试用例执行计划 {
		public static void main(String[] args) {
			Scanner scanner = new Scanner(System.in);
			int N = scanner.nextInt(); // 特性数量
			int M = scanner.nextInt(); // 测试用例数量

			// 读取特性的优先级
			int[] featurePriorities = new int[N];
			for (int i = 0; i < N; i++) {
				featurePriorities[i] = scanner.nextInt();
			}

			// 读取测试用例关联的特性 ID 列表
			List<TestCase> testCases = new ArrayList<>();
			for (int i = 0; i < M; i++) {
				int[] featureIds = new int[scanner.nextInt()];
				for (int j = 0; j < featureIds.length; j++) {
					featureIds[j] = scanner.nextInt() - 1; // 转换为 0-based index
				}
				testCases.add(new TestCase(i + 1, featureIds));
			}

			// 计算测试用例的优先级
			for (TestCase testCase : testCases) {
				testCase.priority = 0;
				for (int featureId : testCase.featureIds) {
					testCase.priority += featurePriorities[featureId];
				}
			}

			// 根据优先级和 ID 对测试用例进行排序
			testCases.sort(Comparator.comparing(TestCase::getPriority).reversed()
				.thenComparing(TestCase::getId));

			// 输出测试用例的 ID
			for (TestCase testCase : testCases) {
				System.out.println(testCase.getId());
			}
		}

		static class TestCase {
			private int id;
			private int[] featureIds;
			private int priority;

			public TestCase(int id, int[] featureIds) {
				this.id = id;
				this.featureIds = featureIds;
			}

			public int getId() {
				return id;
			}

			public int getPriority() {
				return priority;
			}
		}
	}